//
//  AddMessagePhotoCell.m
//  陶瓷
//
//  Created by 王戈 on 15/9/5.
//  Copyright (c) 2015年 王戈. All rights reserved.
//

#import "AddMessagePhotoCell.h"

@interface AddMessagePhotoCell ()

@property (nonatomic, strong) UIButton *deleteBtn;
@property (nonatomic ,copy) DeletePhotoBlock deleteBlock;

@end


@implementation AddMessagePhotoCell

- (void)deleteBtnClick{
    if (_deleteBlock) {
        _deleteBlock(self);
    }
}

- (void)deletePhotoBlock:(DeletePhotoBlock)deleteBlock {
    _deleteBlock = deleteBlock;
}

- (id)initWithCoder:(NSCoder *)aDecoder {
    if ((self = [super initWithCoder:aDecoder])) {
        [self setupChildViews];
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupChildViews];
    }
    return self;
}

- (void)setupChildViews {
    [self.contentView addSubview:self.backgroundImageView];
    [self.contentView addSubview:self.deleteBtn];
}

- (void)setPhotoed:(BOOL)photoed {
    _photoed = photoed;

    _deleteBtn.hidden = !photoed;
    
    if (!photoed) {
        _backgroundImageView.image = [UIImage imageNamed:@"uploadimg"];
    }
}

- (UIButton *)deleteBtn {
    if (_deleteBtn == nil) {
        _deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_deleteBtn setImage:[UIImage imageNamed:@"delete_photo.png"] forState:UIControlStateNormal];
        [_deleteBtn addTarget:self action:@selector(deleteBtnClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _deleteBtn;
}

- (UIImageView *)backgroundImageView {
    if (_backgroundImageView == nil) {
        _backgroundImageView = [[UIImageView alloc] init];
    }
    return _backgroundImageView;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    _deleteBtn.size = _deleteBtn.currentImage.size;
    _deleteBtn.x = self.width - _deleteBtn.width;
    
    _backgroundImageView.frame = self.contentView.bounds;
}
@end
